<template>
	<view class="exam-list-container">
		<uv-empty v-if="list.length === 0" mode="list" text="暂无试卷数据"></uv-empty>
		<uv-list v-else>
			<ExamItem v-for="(item, index) in list" :key="index" :item="item" @click="handleExamClick"></ExamItem>
			<view class="load-more" v-if="hasMore">
				<uv-load-more :status="loadMoreStatus" @loadmore="loadMore"></uv-load-more>
			</view>
		</uv-list>
	</view>
</template>

<script>
	import ExamItem from './ExamItem.vue';
	
	export default {
		name: "ExamList",
		components: {
			ExamItem
		},
		props: {
			// 是否自动加载数据
			autoLoad: {
				type: Boolean,
				default: true
			},
			// 自定义数据源
			dataSource: {
				type: Array,
				default: null
			}
		},
		data() {
			return {
				list: [],
				pageNo: 1,
				pageSize: 10,
				total: 0,
				loadMoreStatus: 'loadmore',
				hasMore: false,
				isLoading: false
			};
		},
		created() {
			// 如果提供了外部数据源，则使用外部数据
			if (this.dataSource) {
				this.list = this.dataSource;
				return;
			}
			
			// 否则自动加载数据
			if (this.autoLoad) {
				this.loadData();
			}
		},
		methods: {
			// 加载试卷列表数据
			loadData(refresh = false) {
				if (this.isLoading) return;
				
				if (refresh) {
					this.pageNo = 1;
					this.list = [];
				}
				
				this.isLoading = true;
				this.loadMoreStatus = 'loading';
				
				this.$api('getExamsList', {
					pageNo: this.pageNo,
					pageSize: this.pageSize
				})
				.then(res => {
					if (refresh) {
						this.list = res.rows;
					} else {
						this.list = [...this.list, ...res.rows];
					}
				})
				
				// 模拟API请求，实际项目中应替换为真实API调用
				// setTimeout(() => {
				// 	// 模拟数据
				// 	const mockData = {
				// 		records: this.getMockData(),
				// 		total: 35,
				// 		pageNo: this.pageNo,
				// 		pageSize: this.pageSize
				// 	};
					
				// 	if (refresh) {
				// 		this.list = mockData.records;
				// 	} else {
				// 		this.list = [...this.list, ...mockData.records];
				// 	}
					
				// 	this.total = mockData.total;
				// 	this.hasMore = this.list.length < this.total;
				// 	this.loadMoreStatus = this.hasMore ? 'loadmore' : 'nomore';
				// 	this.isLoading = false;
					
				// 	// 触发数据加载完成事件
				// 	this.$emit('loaded', {
				// 		list: this.list,
				// 		total: this.total,
				// 		pageNo: this.pageNo,
				// 		pageSize: this.pageSize
				// 	});
				// }, 500);
			},
			
			// 加载更多数据
			loadMore() {
				if (this.isLoading || !this.hasMore) return;
				this.pageNo++;
				this.loadData();
			},
			
			// 刷新数据
			refresh() {
				this.loadData(true);
			},
			
			// 处理试卷点击事件
			handleExamClick(item) {
				this.$emit('exam-click', item);
				// 可以在这里添加导航逻辑，例如：
				// uni.navigateTo({
				//   url: `/pages/exam/detail?id=${item.id}`
				// });
			},
			
			// 生成模拟数据
			getMockData() {
				const types = ['选择题', '填空题', '综合题', '判断题'];
				const categories = ['基础知识', '进阶练习', '综合测试', '模拟考试'];
				const statuses = ['not_started', 'in_progress', 'completed', 'graded'];
				
				const result = [];
				const startIndex = (this.pageNo - 1) * this.pageSize;
				const endIndex = Math.min(startIndex + this.pageSize, 35);
				
				for (let i = startIndex; i < endIndex; i++) {
					// 创建日期（过去30天内的随机日期）
					const createDate = new Date();
					createDate.setDate(createDate.getDate() - Math.floor(Math.random() * 30));
					
					// 开始和结束日期（未来30天内的随机日期）
					const startDate = new Date();
					startDate.setDate(startDate.getDate() + Math.floor(Math.random() * 15));
					
					const endDate = new Date(startDate);
					endDate.setDate(endDate.getDate() + 7 + Math.floor(Math.random() * 15));
					
					// 随机分数设置
					const totalScore = 100;
					const passScore = 60 + Math.floor(Math.random() * 10);
					
					result.push({
						id: i + 1,
						title: `试卷${i + 1}: ${types[i % types.length]}专项练习`,
						type: types[i % types.length],
						category: categories[i % categories.length],
						description: i % 3 === 0 ? `这是一份${types[i % types.length]}专项练习试卷，包含多个难度等级的题目。` : '',
						questionCount: 10 + Math.floor(Math.random() * 20),
						status: statuses[i % statuses.length],
						create_time: this.formatDate(createDate),
						start_time: this.formatDate(startDate),
						end_time: this.formatDate(endDate),
						duration: 60 + Math.floor(Math.random() * 60), // 60-120分钟
						total_score: totalScore,
						pass_score: passScore
					});
				}
				
				return result;
			},
			
			// 格式化日期
			formatDate(date) {
				const year = date.getFullYear();
				const month = String(date.getMonth() + 1).padStart(2, '0');
				const day = String(date.getDate()).padStart(2, '0');
				return `${year}-${month}-${day}`;
			}
		}
	};
</script>

<style lang="scss" scoped>
.exam-list-container {
	padding: 20rpx;
}

.load-more {
	padding: 20rpx 0;
}
</style>